"use strict";
cc._RF.push(module, 'f164f3ciGZD1rVCbAduIMH4', 'JoystickRight');
// script/game/JoystickRight.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var Constants_1 = require("../libs/Constants");
var GameEvent_1 = require("../libs/GameEvent");
var HelperMgr_1 = require("../libs/HelperMgr");
var GameManager_1 = require("./GameManager");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var JoystickRight = /** @class */ (function (_super) {
    __extends(JoystickRight, _super);
    function JoystickRight() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.player = null;
        _this.dot = null;
        _this.ring = null;
        _this.over = null;
        _this.force = null;
        _this.curDir = "";
        _this.faceTo = "K";
        _this.moveDir = new cc.Vec2(0, 1);
        _this._speedType = Constants_1.default.SpeedType.STOP;
        _this.stopSpeed = 0;
        _this.normalSpeed = 200;
        _this.fastSpeed = 200;
        _this.joystickType = Constants_1.default.JoystickType.FOLLOW;
        _this._stickPos = null;
        _this._touchLocation = null;
        _this._radius = null;
        // from self
        _this._moveSpeed = 0;
        _this._movePosi = null;
        _this._force = 0;
        _this._giveup = false;
        //playe getPosition
        _this.playerPos = null;
        _this.beginDotPos = null;
        _this.endDotPos = null;
        _this._isMove = false;
        return _this;
    }
    JoystickRight.prototype.onLoad = function () {
        this.player = GameManager_1.default.instace.player;
        this._radius = this.ring.width / 2;
        this._initTouchEvent();
        if (this.joystickType === Constants_1.default.JoystickType.FOLLOW) {
            this.node.opacity = 0;
        }
    };
    JoystickRight.prototype._initTouchEvent = function () {
        this.node.on(cc.Node.EventType.TOUCH_START, this._touchStartEvent, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this._touchMoveEvent, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this._touchEndEvent, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this._touchEndEvent, this);
    };
    JoystickRight.prototype._touchStartEvent = function (event) {
        var touchPos = this.node.convertToNodeSpaceAR(event.getLocation());
        if (this.joystickType === Constants_1.default.JoystickType.FIXED) {
            this._stickPos = this.ring.getPosition();
            // 触摸点与圆圈中心的距离
            var distance = touchPos.sub(this.ring.getPosition()).mag();
            // 手指在圆圈内触摸,控杆跟随触摸点
            this._radius > distance && this.dot.setPosition(touchPos);
        }
        else if (this.joystickType === Constants_1.default.JoystickType.FOLLOW) {
            // 记录摇杆位置，给 touch move 使用
            this._stickPos = touchPos;
            this.node.opacity = 255;
            this._touchLocation = event.getLocation();
            // 更改摇杆的位置
            this.ring.setPosition(touchPos);
            // this.over.setPosition(touchPos.x + 150, touchPos.y + 100)
            this.dot.setPosition(touchPos);
            this.beginDotPos = touchPos;
        }
        this._isMove = true;
    };
    JoystickRight.prototype._touchMoveEvent = function (event) {
        // 如果 touch start 位置和 touch move 相同，禁止移动
        if (this.joystickType === Constants_1.default.JoystickType.FOLLOW && this._touchLocation === event.getLocation()) {
            return false;
        }
        // 以放弃圆圈为锚点获取触摸坐标
        var touchPos1 = this.over.convertToNodeSpaceAR(event.getLocation());
        var distance1 = touchPos1.mag();
        this._giveup = distance1 <= 30;
        // 以圆圈为锚点获取触摸坐标
        var touchPos2 = this.ring.convertToNodeSpaceAR(event.getLocation());
        var distance2 = touchPos2.mag();
        // 由于摇杆的 postion 是以父节点为锚点，所以定位要加上 touch start 时的位置
        var posX = this._stickPos.x + touchPos2.x;
        var posY = this._stickPos.y + touchPos2.y;
        // 归一化
        var p = cc.v2(posX, posY).sub(this.ring.getPosition()).normalize();
        var speedType;
        if (p.y > 0) {
            p.y = 0;
        }
        if (this._radius > distance2) {
            this.dot.setPosition(cc.v2(posX, posY));
            speedType = Constants_1.default.SpeedType.NORMAL;
        }
        else {
            // 控杆永远保持在圈内，并在圈内跟随触摸更新角度
            var x = this._stickPos.x + p.x * this._radius;
            var y = this._stickPos.y + p.y * this._radius;
            this.dot.setPosition(cc.v2(x, y));
            this.endDotPos = cc.v2(x, y);
            speedType = Constants_1.default.SpeedType.FAST;
        }
        this.onTouchMove(event, { speedType: speedType, moveDistance: p });
    };
    JoystickRight.prototype._touchEndEvent = function (event) {
        this.dot.setPosition(this.ring.getPosition());
        if (this.joystickType === Constants_1.default.JoystickType.FOLLOW) {
            this.node.opacity = 0;
        }
        this.onTouchEnd(event, { speedType: Constants_1.default.SpeedType.STOP });
        this._isMove = false;
    };
    JoystickRight.prototype.onTouchMove = function (event, data) {
        // if (DataMgr.meetUser) return;
        this._speedType = data.speedType;
        this.moveDir = data.moveDistance;
        this.setDir(data.moveDistance.x);
        if (this.beginDotPos && this.endDotPos) {
            if (GameManager_1.default.instace.playerIsStosh) {
                var offset = new cc.Vec2(this.endDotPos.x - this.beginDotPos.x, this.endDotPos.y - this.beginDotPos.y);
                GameEvent_1.GameEvent.emit(Constants_1.default.StageEvent.DrawParabola, offset);
            }
            else {
                GameEvent_1.GameEvent.emit(Constants_1.default.StageEvent.RotateRod, HelperMgr_1.default.getAngele(this.endDotPos.x, this.endDotPos.y, this.beginDotPos.x, this.beginDotPos.y));
            }
        }
    };
    //设置朝向
    JoystickRight.prototype.setDir = function (dir) {
        var scale = this.player.scaleY;
        this.player.scaleX = dir > 0 ? scale : -scale;
    };
    JoystickRight.prototype.update = function (dt) {
        switch (this._speedType) {
            case Constants_1.default.SpeedType.STOP:
                this._moveSpeed = this.stopSpeed;
                break;
            case Constants_1.default.SpeedType.NORMAL:
                this._moveSpeed = this.normalSpeed;
                break;
            case Constants_1.default.SpeedType.FAST:
                this._moveSpeed = this.fastSpeed;
                break;
            default:
                break;
        }
        if (this._isMove) {
            if (!this._giveup) {
                this._force += 0.5;
            }
            if (this._force > 100) {
                this._force = 100;
            }
            if (this.force) {
                this.force.string = this._giveup ? "放弃" : Math.round(this._force) + "%";
            }
        }
        else {
            this._force = 0;
        }
    };
    JoystickRight.prototype.onTouchEnd = function (event, data) {
        if (GameManager_1.default.instace.playerIsStosh) {
            GameEvent_1.GameEvent.emit(Constants_1.default.StageEvent.ClearParabola);
        }
        if (this._giveup) {
            GameEvent_1.GameEvent.emit(Constants_1.default.StageEvent.ResetRod);
            return;
        }
        this._speedType = data.speedType;
        if (this.beginDotPos && this.endDotPos) {
            GameManager_1.default.instace.forcePercent = this._force;
            if (GameManager_1.default.instace.playerIsStosh) {
                GameEvent_1.GameEvent.emit(Constants_1.default.StageEvent.CastStosh);
            }
            else {
                GameEvent_1.GameEvent.emit(Constants_1.default.StageEvent.LaunchRod);
            }
        }
    };
    __decorate([
        property({
            type: cc.Node,
            displayName: 'Dot',
            tooltip: '摇杆操纵点',
        })
    ], JoystickRight.prototype, "dot", void 0);
    __decorate([
        property({
            type: cc.Node,
            displayName: 'Ring',
            tooltip: '摇杆背景节点',
        })
    ], JoystickRight.prototype, "ring", void 0);
    __decorate([
        property({
            type: cc.Node,
            displayName: 'Over',
            tooltip: '放弃节点',
        })
    ], JoystickRight.prototype, "over", void 0);
    __decorate([
        property({
            type: cc.Label,
            displayName: 'force',
            tooltip: '摇杆力的大小',
        })
    ], JoystickRight.prototype, "force", void 0);
    __decorate([
        property({
            tooltip: 'curDir',
        })
    ], JoystickRight.prototype, "curDir", void 0);
    __decorate([
        property({
            tooltip: 'faceTo',
        })
    ], JoystickRight.prototype, "faceTo", void 0);
    __decorate([
        property({
            displayName: 'Move Dir',
            tooltip: '移动方向',
        })
    ], JoystickRight.prototype, "moveDir", void 0);
    __decorate([
        property({
            type: cc.Integer,
            tooltip: '停止时速度',
        })
    ], JoystickRight.prototype, "stopSpeed", void 0);
    __decorate([
        property({
            type: cc.Integer,
            tooltip: '正常速度',
        })
    ], JoystickRight.prototype, "normalSpeed", void 0);
    __decorate([
        property({
            type: cc.Integer,
            tooltip: '最快速度',
        })
    ], JoystickRight.prototype, "fastSpeed", void 0);
    __decorate([
        property({
            type: cc.Enum(Constants_1.default.JoystickType),
            displayName: 'joystickType',
            tooltip: 'Touch Type',
        })
    ], JoystickRight.prototype, "joystickType", void 0);
    __decorate([
        property({
            tooltip: '摇杆所在位置',
        })
    ], JoystickRight.prototype, "_stickPos", void 0);
    __decorate([
        property({
            tooltip: '触摸位置',
        })
    ], JoystickRight.prototype, "_touchLocation", void 0);
    JoystickRight = __decorate([
        ccclass
    ], JoystickRight);
    return JoystickRight;
}(cc.Component));
exports.default = JoystickRight;

cc._RF.pop();